<template>
	<view class="page">
		<uni-forms :modelValue="formData" label-width="200rpx">
			<uni-forms-item label="项目" name="order">
				<view @click="toOrders">
					<uni-easyinput type="text" v-model="formData.order.name" disabled />
				</view>
			</uni-forms-item>
			<uni-forms-item label="扫描箱体码" name="syscode">
				<uni-easyinput type="text" v-model="formData.syscode" placeholder="请输入箱体码" suffixIcon="scan"
					@iconClick="iconClick1" />
			</uni-forms-item>
			<uni-forms-item label="站点名称" name="sname">
				<uni-easyinput type="text" v-model="formData.sname" placeholder="请输入站点名称" />
			</uni-forms-item>
			<uni-forms-item label="街道" name="street">
				<uni-easyinput type="text" v-model="formData.street" placeholder="请输入街道" />
			</uni-forms-item>
			<uni-forms-item label="方向" name="dir">
				<uni-data-select v-model="formData.dir" placeholder="请选择站点位置" :localdata="dirs"></uni-data-select>
			</uni-forms-item>
			<uni-forms-item label="线路" name="lname">
				<uni-easyinput type="texteare" v-model="formData.lname" placeholder="请选择线路" suffixIcon="list"
					@iconClick="iconClick2" disabled />
			</uni-forms-item>
			<uni-forms-item label="定位" name="locate">
				<uni-easyinput type="text" v-model="formData.address" disabled/>
			</uni-forms-item>
		</uni-forms>
		<u-button class="btn" @click="submit" type="primary" text="提交" />
		<view>
			<!-- 提示窗示例 -->
			<uni-popup ref="alertDialog" type="dialog">
				<uni-popup-dialog :type="msgType" cancelText="否" confirmText="是" title="通知" content="是否同步更新终端线路文件!"
					@confirm="dialogConfirm" @close="dialogClose"></uni-popup-dialog>
			</uni-popup>
		</view>
	</view>
</template>

<script>
	import {
		site
	} from '@/api/common.js';
	import {
		line
	} from '@/api/common.js';
	export default {
		data() {
			return {
				formData: {
					syscode: '',
					order: {
						name: '请先选订单'
					},
					lname: '',
					dir: '',
					street: '',
					sname: '',
					address:'0,0',
					lat:'0',
					lon:'0'
				},
				msgType: 'success',
				dirs: [{
						value: '路东',
						text: "路东"
					},
					{
						value: '路西',
						text: "路西"
					},
					{
						value: '路南',
						text: "路南"
					},
					{
						value: '路北',
						text: "路北"
					},
				],
			}
		},
		onLoad() {
			this.toLocate()
			uni.getStorage({
				key: 'order',
				success: (e) => {
					this.formData.order = e.data
				},
				fail() {
					uni.showToast({
						title: '请先选订单',
						icon: 'none'
					});
				}
			})
		},

		onShow() {
			let that = this
			uni.$on('order', function(data) {
				console.log('监听到事件来自 testdata ，携带参数为：' + JSON.stringify(data));
				if (that.formData.order.id != data.id) {
					console.log('订单修改....')
					that.formData.order = data
				}
			})
			uni.$on('updateData', function(data) {
				let lid = ''
				let lno = ''
				let lname = ''
				data.forEach(item => {
					lid += item.id + ","
					lno += item.lno + ","
					lname += item.name + ","
				})
				if (lid.endsWith(",")) {
					lid = lid.substring(0, lid.length - 1)
				}
				if (lno.endsWith(",")) {
					lno = lno.substring(0, lno.length - 1)
				}
				if (lname.endsWith(",")) {
					lname = lname.substring(0, lname.length - 1)
				}
				that.formData.lid = lid
				that.formData.lno = lno
				that.formData.lname = lname
				console.log('onShow监听到事件来自 updateData ，携带参数为：' + JSON.stringify(data));
			})
		},
		methods: {
			toOrders() {
				console.log('准备跳转项目列表页面')
				uni.navigateTo({
					url: '/pages/order_select/order_select'
				})
			},
			iconClick1(type) {
				console.log('點擊了掃碼。。。')
				// 允许从相机和相册扫码
				var that = this
				uni.scanCode({
					success: function(res) {
						console.log('条码类型：' + res.scanType);
						console.log('条码内容：' + res.result);
						that.formData.syscode = res.result
					},
					fail() {
						uni.showToast({
							icon: 'none',
							title: '此设备暂不支持扫码功能',
						})
					}
				});
			},
			//线路配置
			iconClick2(type) {
				if (this.formData.syscode == '') {
					uni.showToast({
						title: '请先扫描设备码',
						icon: 'none'
					});
				} else {
					uni.navigateTo({
						url: '/pages/device_line/device_line?order=' + this.formData.order.id + "&sid=" + this
							.formData
							.syscode
					})
				}

			},
			submit() {
				let param = this.formData;
				param.sid = this.formData.syscode
				site(param).then(res => {
					console.log(res);
					uni.showToast({
						title: "修改成功",
						icon: 'none'
					});
					setTimeout(() => {
						uni.navigateBack();
					}, 500)
					// this.dialogToggle("success")
				}).catch(err => {
					console.log(err);
					uni.showToast({
						title: err,
						icon: 'none'
					});
				})
			},
			toLocate() {
				console.log('准备定位：');
				var that = this
				uni.getLocation({
					type: 'wgs84',
					success: function(res) {
						console.log('当前位置的经度：' + res.longitude);
						console.log('当前位置的纬度：' + res.latitude);
						that.formData.address = res.longitude + ',' + res.latitude
					},
					fail() {
						console.log('定位失敗')
					}
				});
			},
			dialogToggle(type) {
				this.msgType = type
				this.$refs.alertDialog.open()
			},
			dialogConfirm() {
				console.log('点击确认');
				let param = {};
				param.pid = this.pid;
				param.sid = this.formData.sid;
				line(param).then(res => {
					console.log(res.Message);
					uni.showToast({
						title: res.Message,
						icon: 'none'
					});
				}).catch(err => {
					console.log(err);
					uni.showToast({
						title: err,
						icon: 'none'
					});
				})
			},
			dialogClose() {
				console.log('点击关闭')
			},
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		margin-top: 32rpx;
		width: 710rpx;
		margin-left: 16rpx;
		margin-right: 16rpx;
	}
</style>